package com.sourceQ.common.mybatis.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.sourceQ.common.security.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;


/**
 * @author: SourceQ
 * @create: 2024-04-01 17:25
 * @Description:
 */
@Component
public class CreateOrUpdateMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        LocalDateTime date = LocalDateTime.now();
        metaObject.setValue("createTime", date);
        metaObject.setValue("updateTime", date);
        metaObject.setValue("createBy", SecurityUtils.getLoginUser().getUsername());
        metaObject.setValue("updateBy", SecurityUtils.getLoginUser().getUsername());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateBy", SecurityUtils.getLoginUser().getUsername());

    }
}
